package acl.hibernate.dao;

import acl.hibernate.po.OaRoles;
import acl.hibernate.po.OaRolesPermissions;
import acl.hibernate.po.OaUsersRoles;
import hibernate.HibernateBaseDAO;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Rolesdao extends HibernateBaseDAO{
	// 实例化一个session工厂
	
	// ----------------------------------------------------------
	public List getRolesByCreatUserId(Integer id) {// 根据用户id获取他所创建的角色
		// Author:Never
		List result=null;
		try {
			session = this.getSession();
			String hql = "From OaRoles as role WHERE role.userId=" + id;
			Query q = session.createQuery(hql);
			result = q.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{			
			this.closeSession(session);
		}
		return result;
	}

	public void deleteRoleById(Integer id) {// 根据id删除角色
		// Author:Never
		session = this.getSession();
		String hql = "From OaRoles as role WHERE role.id=" + id;
		Query q = session.createQuery(hql);
		List list = q.list();
		OaRoles oaRole = (OaRoles) list.get(0);
		Transaction tr = session.beginTransaction();
		//要注意同批事务处理问题
		try{
			session.delete(oaRole);			
			tr.commit();
		}
		catch(Exception e){
			if(tr!=null)
				tr.rollback();
			e.printStackTrace();
			this.closeSession(session);
			return;
		}
		this.closeSession(session);		
		deleteRoles_Permissions(id);
		deleteUsers_Roles(id);
	}
	public void deleteCheckedRolesByIds(String ids) {
		// 批量删除角色
		// Author:Never
		session = this.getSession();
		String[] id = ids.split(",");
		Transaction tr = null;
		try{
			tr=session.beginTransaction();
			for (int i = 0; i < id.length; i++) {
				if (!id[i].equals("-1")) {					
					String hql = "From OaRoles as role WHERE role.id=" + id[i];
					Query q = session.createQuery(hql);
					List list = q.list();
					OaRoles oaRole = (OaRoles) list.get(0);
					session.delete(oaRole);				
					this.closeSession(session);
				}
			}
			tr.commit();
		}catch(Exception e){
			if(tr!=null)
				tr.rollback();
			e.printStackTrace();
		}
		for (int i = 0; i < id.length; i++) {// 删除角色权限映射
			if (!id[i].equals("-1")) {
				deleteRoles_Permissions(Integer.parseInt(id[i]));
				deleteUsers_Roles(Integer.parseInt(id[i]));
			}
		}
	}
	public void deleteCheckedRolesByIds(String[] ids) {
		// 批量删除角色
		// Author:Never
		session = this.getSession();		
		for (int i = 0; i < ids.length; i++) {
			if (!ids[i].equals("-1")) {				
				Transaction tr = null;
				try{
					tr=session.beginTransaction();
					String hql = "From OaRoles as role WHERE role.id=" + ids[i];
					Query q = session.createQuery(hql);
					List list = q.list();
					OaRoles oaRole = (OaRoles) list.get(0);
					session.delete(oaRole);
					tr.commit();
				}
				catch(Exception e){
					if(tr!=null)
						tr.rollback();
					e.printStackTrace();
					this.closeSession(session);
					return;
				}
				finally{
					this.closeSession(session);
					
				}				
			}
		}		
		for (int i = 0; i < ids.length; i++) {// 删除角色权限映射
			if (!ids[i].equals("-1")) {
				deleteRoles_Permissions(Integer.parseInt(ids[i]));
				deleteUsers_Roles(Integer.parseInt(ids[i]));
			}
		}
	}
	public void deleteRoles_Permissions(Integer role_id) {// 根据角色id删除角色权限映射
		// Author:Never
		session = this.getSession();
		
		String hql = "From OaRolesPermissions as rolespermissions WHERE rolespermissions.roleId="
				+ role_id;
		Query q = session.createQuery(hql);
		List list = q.list();
		session.close();
		
		session = this.getSession();
		Transaction tr = null;
		try{
			tr=session.beginTransaction();
			Iterator iterator = list.iterator();
			while (iterator.hasNext()) {		
				OaRolesPermissions rp = (OaRolesPermissions) iterator.next();
				session.delete(rp);		
			}
			tr.commit();
		}
		catch(Exception e){
			if(tr!=null)
				tr.rollback();
			e.printStackTrace();
			this.closeSession(session);
			return;
		}finally{
			this.closeSession(session);
		}		
	}
	public List getRolesByUserId(Integer id){
		//获取用户所拥有的角色
		// Author:Silence
		session = this.getSession();
		String hql = "From OaUsersRoles as roles WHERE roles.userId="+id;
		Query q = session.createQuery(hql);
		//q.setInteger("id", id);
		List roles = q.list();
		this.closeSession(session);
		return roles;
		
	}
	public List getRolesByRolesId(List userRoles){
		//获取角色信息通过角色Id
		// Author:Silence
		session = this.getSession();
		List roles = new ArrayList();
		String hql = null;
		if(userRoles.size()==0)
			return null;
		if(userRoles.size()!=0){
			hql = "From OaRoles WHERE id="+((OaUsersRoles)userRoles.get(0)).getRoleId();
		}
		for(int i = 1;i<userRoles.size();i++){
			hql+="or id="+((OaUsersRoles)userRoles.get(i)).getRoleId();
		}
		Query q = session.createQuery(hql);
		roles = q.list();
		this.closeSession(session);
		return roles;
		
	}
	public boolean deleteRolesHased(Integer userId,String id){
		//根据用户id和角色id，删除用户角色
		// Author:Silence
		String[] ids = id.split(",");
		session = this.getSession();
		Transaction tr = null;
		try{
			tr=session.beginTransaction();
			for(int i = 0;i<ids.length;i++){
				if(ids[i]!="-1"){				
					String hql = "DELETE OaUsersRoles WHERE roleId="+Integer.valueOf(ids[i])+"and userId="+userId;
					Query q = this.session.createQuery(hql);
					q.executeUpdate();							
				}
			}
			tr.commit();
			return true;
		}catch(Exception e){
			if(tr!=null)
				tr.rollback();
			e.printStackTrace();
			this.closeSession(session);
			return false;
		}			
	}
	public boolean givePersonRoles(Integer userId,String id){
		//根据用户id和角色id，添加用户角色
		// Author:Silence
		String[] ids = id.split(",");
		session = this.getSession();		
		OaUsersRoles userRole = new OaUsersRoles();
		Date d = new Date();
		Transaction tr = null;
		try{
			tr=session.beginTransaction();
			for(int i = 0;i<ids.length;i++){
				if(ids[i]!="-1"){				
					userRole.setRoleId(Integer.valueOf(ids[i]));
					userRole.setUserId(userId);
					userRole.setCreated(d);
					userRole.setModified(d);			
					session.save(userRole);	
				}
			}
			tr.commit();
			return true;
		}catch(Exception e){
			if(tr!=null)
				tr.rollback();
			e.printStackTrace();
			this.closeSession(session);
			return false;
		}	
		
	}
	public void deleteUsers_Roles(Integer role_id) {// 根据角色id删除用户角色映射
		// Author:Never
		session = this.getSession();		
		String hql = "From OaUsersRoles as oaUsersRoles WHERE oaUsersRoles.roleId="
				+ role_id;
		Query q = session.createQuery(hql);
		List list = q.list();
		session.close();
		session=this.getSession();
		Iterator iterator = list.iterator();
		Transaction tr = null;
		try{
			tr=session.beginTransaction();
			while (iterator.hasNext()) {						
				OaUsersRoles ur = (OaUsersRoles) iterator.next();
				session.delete(ur);						
			}	
			tr.commit();
		}catch(Exception e){
			if(tr!=null)
				tr.rollback();
			this.closeSession(session);
			return;
		}
		this.closeSession(session);		
	}
	public OaRoles getRoleByRoleid(Integer roleid) {// 根据角色id获取角色信息
		// Author:Never
		session = this.getSession();
		String hql = "From OaRoles as roles WHERE roles.id="+ roleid;
		Query q = session.createQuery(hql);
		List list = q.list();
		OaRoles or=(OaRoles)list.get(0);
		this.closeSession(session);
		return or;
	}
	public OaRoles getRoleByRolename(String roleName) {// 根据角色名获取角色信息
		// Author:Never
		session = this.getSession();
		OaRoles or=null;
		try{
			String hql = "From OaRoles as role WHERE role.rolename=:roleName";
			Query q = session.createQuery(hql);
			q.setString("roleName", roleName);
			List list = q.list();
			if(list.size()>0)
			or=(OaRoles)list.get(0);
		}catch (Exception e){
			e.printStackTrace();
			}
		finally{
			this.closeSession(session);
		}
		return or;
	}
	
	public void saveOrUpdateRoleInfo(OaRoles oaRoles) {//保存或更新用户信息
		// Author:Never
		session = this.getSession();
		Transaction tr =null;
		try {			
			tr=session.beginTransaction();
			session.saveOrUpdate(oaRoles);
			tr.commit();
		} catch (RuntimeException re) {
			if(tr!=null)
				tr.rollback();
			re.printStackTrace();
		}
		finally{
			this.closeSession(session);
		}
	}
	
	public void saveOrUpdateRolesPermissions(OaRolesPermissions oaRolesPermissions) {//保存或更新用户信息
		// Author:Never
		session = this.getSession();
		Transaction tr=null;
		try {			
			tr =session.beginTransaction();
			session.saveOrUpdate(oaRolesPermissions);
			tr.commit();
		} catch(Exception e){
			if(tr!=null)
				tr.rollback();
			this.closeSession(session);
			return;
		}
		finally{
			this.closeSession(session);
		}
	}
}
